<?php

class Ext_Service_Qq
{
    const URI_BASE = 'https://graph.qq.com/';

    public $accessToken;
    public $openid;

    protected $_restClient = null;

    public function __construct($accessToken, $openid)
    {
        $this->accessToken = (string) $accessToken;
        $this->openid      = (string) $openid;
    }

    public function getUser()
    {
        $config = Ext_Config::getInstance()->getConfig("oauth");
        $consumerKey = $config->adapter->qq->consumerKey;
        
        $options = array(
            'access_token'       => $this->accessToken, 
            'oauth_consumer_key' => $consumerKey, 
            'openid'             => $this->openid,
            'format'             => "json",
        );
        $options = array_map("urlencode",$options);
        //Zend_Debug::dump($options);
        $restClient = $this->getRestClient();
        $restClient->getHttpClient()->resetParameters();
        $response = $restClient->restPost('/user/get_user_info', $options);
        //Zend_Debug::dump($response->getBody());
        if ($response->isError()) {
            require_once 'Ext/Service/Exception.php';
            throw new Ext_Service_Exception(
                'An error occurred sending request. Status code: ' . $response->getStatus()
            );
        }

        return Zend_Json::decode($response->getBody());
    }

    public function getRestClient()
    {
        if (null === $this->_restClient) {
            /**
             * @see Zend_Rest_Client
             */
            require_once 'Zend/Rest/Client.php';
            $this->_restClient = new Zend_Rest_Client(self::URI_BASE);
        }

        return $this->_restClient;
    }
}

